package com.tech.view.panel;

import com.tech.repository.dto.GoodsInfo;
import com.tech.repository.entity.Goods;
import com.tech.util.LogProcessUtil;
import com.tech.util.TestDataUtil;
import com.tech.view.common.*;
import com.tech.view.frame.FrameManager;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.VerticalLayout;
import org.jdesktop.swingx.graphics.ReflectionRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.logging.Logger;

/**
 * 商品卡片面板
 */
public class GoodsCardPanel extends ImageJPanel implements KeyListener {
	private final static Logger LOG = LogProcessUtil.getLogger(GoodsCardPanel.class.getName());
	private final static ReflectionRenderer REFLECTION_RENDERER = new ReflectionRenderer();
	public static int DEFAULT_WIDTH = 190;
	/**
	 * 图像的最大高度，图像不会超过这个高度
	 */
	public static int MAX_HEIGHT = 336;
	// 图片下方文本区域的高度
	public static int TEXT_HEIGHT = 95;

	static {
		// 设置模糊效果
		REFLECTION_RENDERER.setBlurEnabled(true);
	}

	private int width;
	/**
	 * 高度依据图像大小进行计算
	 */
	private int height;
	private FrameManager frameManager;
	private GoodsInfo goodsInfo;
	private JXLabel goodsTitle;
	private JPanel goodsLabelPanel;
	private JPanel goodsPricePanel;
	private JXLabel goodsSeller;

	public GoodsCardPanel(GoodsInfo goodsInfo) {
		this(DEFAULT_WIDTH, goodsInfo);
	}

	public GoodsCardPanel(int width, GoodsInfo goodsInfo) {
		this.width = width;
		this.goodsInfo = goodsInfo;
		this.frameManager = FrameManager.getInstance();
		initComponents();
		setListeners();
		initView();
	}

	public static void main(String[] args) {
		// GoodsCardPanel panel = new GoodsCardPanel(TestDataUtil.GOODS_INFO_EMPTY_IMAGE);
		GoodsCardPanel panel = new GoodsCardPanel(TestDataUtil.GOODS_INFO);
		SwingTestUtils.test(panel);
	}

	private void initComponents() {
		Goods goods = goodsInfo.getGoods();
		BufferedImage firstImage = goodsInfo.getFirstImage();
		// 设置初始高度
		height = TEXT_HEIGHT;
		if (firstImage != null) {
			// 缩放
			firstImage = GraphicsUtil.createFixedWidthScaledCompositeInstance(firstImage, width, MAX_HEIGHT);
			// 圆角处理
			firstImage = ImageUtil.createRoundImage(firstImage, 15);
			// 重新设置面板高度，应该在添加反射前设置，因为反射会增大图像高度
			height += firstImage.getHeight();
			// 添加反射
			firstImage = REFLECTION_RENDERER.appendReflection(firstImage);
			setBackgroundImage(firstImage);
		}

		goodsTitle = new JXLabel(goods.getGname());
		goodsTitle.setFont(FontUtil.TITLE_FONT);
		goodsTitle.setForeground(ColorUtil.TITLE_FONT_COLOR);

		// JXLabel goodsLabel = new JXLabel(goods.getGcondition().toString());
		// goodsLabel.setForeground(ColorUtil.RED_LABEL_COLOR);
		// goodsLabel.setBorder(BorderFactory.createLineBorder(ColorUtil.RED_LABEL_COLOR, 1, true));

		goodsLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
		goodsLabelPanel.setOpaque(false);
		goodsLabelPanel.add(LabelFactory.createLabel(goods.getGcondition().toString(), null, ColorUtil.RED_LABEL_COLOR));

		JXLabel goodsPrice = new JXLabel(String.format("￥%.2f", goods.getGprice()));
		goodsPrice.setFont(FontUtil.TITLE_FONT);
		goodsPrice.setForeground(ColorUtil.RED_LABEL_COLOR);
		JXLabel goodsPopularity = new JXLabel("人气：" + goods.getGpopularity());
		goodsPricePanel = new JPanel(new BorderLayout(5, 0));
		goodsPricePanel.setOpaque(false);
		goodsPricePanel.add(goodsPrice, BorderLayout.WEST);
		goodsPricePanel.add(goodsPopularity, BorderLayout.EAST);

		goodsSeller = new JXLabel(goods.getUname());
		goodsSeller.setFont(FontUtil.SMALL_FONT);
	}

	private void initView() {
		setLayout(new VerticalLayout());
		setPreferredSize(new Dimension(width, height));

		int textTop = height - TEXT_HEIGHT;
		add(Box.createVerticalStrut(textTop));
		add(goodsTitle);
		add(goodsLabelPanel);

		add(goodsPricePanel);
		add(goodsSeller);

	}

	private void setListeners() {
		addKeyListener(this);
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// 跳转到商品详情页
	}

	@Override
	public void keyPressed(KeyEvent e) {

	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	public GoodsInfo getGoodsInfo() {
		return goodsInfo;
	}

	public void setGoodsInfo(GoodsInfo goodsInfo) {
		this.goodsInfo = goodsInfo;
	}
}
